package com.sohoometer.assignmeter.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nari.basic.model.IUser;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.ModelDriven;
import com.sohoometer.assignmeter.bean.AssignMeterDataExceptionQuery;
import com.sohoometer.assignmeter.service.AssignMeterDataExceptionQueryService;
import com.sohoometer.base.action.BaseAction;

@Controller
@Scope("prototype")
public class AssignMeterDataExceptionQueryAction extends BaseAction implements ModelDriven<AssignMeterDataExceptionQuery>{
	private static final long serialVersionUID = 1L;
	@Autowired
	private AssignMeterDataExceptionQueryService assignMeterDataExceptionQueryService = null;
	private AssignMeterDataExceptionQuery queryBean  = new AssignMeterDataExceptionQuery();
	private ResultWithPageCond<AssignMeterDataExceptionQuery> querylist;
	
	public String assignMeterDataExceptionQueryList() throws Exception {
		queryConditionList();
		queryMap();
		pageCond =new PageCond();
		//设置为Null
		setBeanNull();
		//设置登陆用户ID
		IUser user = getUser();
		queryBean.setUserId(user.getId());
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		String[] order = new String[] {"queryTime","desc"};
		ArrayList<String[]> orderlist = new ArrayList<String[]>();
		orderlist.add(order);
		pageCond.setOrderList(orderlist);
		querylist = assignMeterDataExceptionQueryService.queryEntityListEqualTWithPageExt(queryBean, pageCond);
		this.pageSize = new BigDecimal(querylist.getPageCond().getTotal()).divide(new BigDecimal(querylist.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "list";
	}
	private void setBeanNull() {
		String projectID = queryBean.getProjectID();
		String communityID = queryBean.getCommunityID();
		String buildingID = queryBean.getBuildingID();
		if(StringUtils.isBlank(projectID)) {
			queryBean.setProjectID(null);
		}
		if(StringUtils.isBlank(communityID)) {
			queryBean.setCommunityID(null);
		}
		if(StringUtils.isBlank(buildingID)) {
			queryBean.setBuildingID(null);
		}
	}
	/**
	 * 增加查询任务页面
	 * @throws Exception 
	 * */
	public String addQueryTask() throws Exception{
		queryConditionList();
		return "addPage";
	}
	/**
	 * 增加查询任务条目
	 * */
	public String addQuery() throws Exception{
		IUser user = getUser();
		if(user==null) {
			rmsg = ResponseMsg.getInstance(false, "请重新登陆后进行创建任务！"); 
			return null;
		}
		Date startRecordTime = queryBean.getStartRecordTime();
		Date endRecordTime = queryBean.getEndRecordTime();
		if(startRecordTime==null||endRecordTime==null) {
			rmsg = ResponseMsg.getInstance(false, "请确定结束查询时间填写正确！"); 
			writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
			return null;
		}else {
			if(endRecordTime.before(startRecordTime)) {
				rmsg = ResponseMsg.getInstance(false, "请确定开始查询时间在结束查询时间之前！"); 
				writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
				return null;
			} 
			long endTime = endRecordTime.getTime();
			long startTime = startRecordTime.getTime();
           long	interval = endTime-startTime;
           long day = interval/(24*60*60*1000);
           if(day>7) {
        	   //查询时间超过七天 则不进行查询
				rmsg = ResponseMsg.getInstance(false, "由于数据量巨大,查询时间不应超过七天"); 
				writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
				return null;
           }
		}
		queryBean.setUserId(user.getId());
		queryBean.setQueryTime(new Date());
		queryBean.setIsdone(Boolean.FALSE);
		//先保存 再执行
		String id = assignMeterDataExceptionQueryService.saveEntity(queryBean);
		assignMeterDataExceptionQueryService.dealWithQuery(queryBean, id);
		rmsg = ResponseMsg.getInstance(true, "任务已成功创建,执行中,请耐心等待...."); 
		writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
		return null;
	}
	/**
	 *查询某一查询任务的详细信息 
	 * */
	public String  queryDetail() throws Exception{
		queryMap();
		AssignMeterDataExceptionQuery queryEntityById = assignMeterDataExceptionQueryService.queryEntityById(queryBean.getId());
		BeanUtils.copyProperties(queryBean, queryEntityById);
		return "detail";
	}
	
	public String deleteQuery() throws Exception{
		String id = queryBean.getId();
		try {
			assignMeterDataExceptionQueryService.deleteQuery(id);
			ResponseMsg msg = ResponseMsg.getInstance(true, "删除成功！");
			writeJsonStrToResponse(JSONUtil.serialize(msg));
		} catch (Exception e) {
			ResponseMsg msg = ResponseMsg.getInstance(false, "未能成功删除,请确认目标存在！");
			writeJsonStrToResponse(JSONUtil.serialize(msg));
		}
		return null;
	}
	@Override
	public AssignMeterDataExceptionQuery getModel() {
		return queryBean;
	}
	public ResultWithPageCond<AssignMeterDataExceptionQuery> getQuerylist() {
		return querylist;
	}
	public void setQuerylist(
			ResultWithPageCond<AssignMeterDataExceptionQuery> querylist) {
		this.querylist = querylist;
	}
	
}
